// video add controller
var app = angular.module('app');

app.controller('bookAddCtrl',
    ['$scope', '$state', '$translate', 'uploadFileService', 'confirmDialogService',
        'bookService', 'status', 'toasterService',
        function ($scope, $state, $translate, uploadFileService, confirmDialogService,
                  bookService, status, toasterService) {

            $scope.status = status;
            $scope.book = {};

            // delete pic
            $scope.delPic = function () {
                $scope.bookPic = null;
                $scope.book.pic = null;
            };

            // clear error
            function clearErr() {
                $scope.addInformationErr = null;
                $scope.uploadPicErrMsg = null;
                $scope.contentErrMsg = null;
            }

            $scope.complete = function () {
                clearErr();
                if (!$scope.form.$valid) {
                    return;
                }

                $scope.filterAuthorTranslatorTag($scope.book);

                bookService.add($scope.book).then(function (data) {
                    $state.go('app.book.list');
                    toasterService.pop('success', null, $translate.instant('strings.book.add.success'));
                }, function (error) {
                    $scope.addInformationErr = error;
                    toasterService.pop('error', null, $translate.instant('strings.book.add.failure'));
                });
            };
        }]);